<?php
/**
 * @title 133. 克隆图
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

/**
 * Definition for a Node.
 * class Node {
 *     public $val = null;
 *     public $neighbors = null;
 *     function __construct($val = 0) {
 *         $this->val = $val;
 *         $this->neighbors = array();
 *     }
 * }
 */

class Solution {
    /**
     * @param Node $node
     * @return Node
     */
    function cloneGraph($node) {
        /**
         * @since 2023-12-28 空节点
         */
        if($node === null) {
            return null;
        } else {}

        /**
         * @since 2023-12-28 Node映射集合，[val] => Node
         */
        $nodeMap = [];
        $nodeMap[$node->val] = new Node($node->val);

        /**
         * @since 2023-12-28 迭代
         */
        $nodeArray = [$node];
        while($nodeArray){
            /**
             * @since 2023-12-28 下次遍历集合
             */
            $nextNodeArray = [];

            /**
             * @since 2023-12-28 遍历当前集合
             */
            for($i=0; $i<count($nodeArray); $i++){
                /**
                 * @since 2023-12-28 附近点集合
                 */
                $neighborArray = [];
                foreach($nodeArray[$i]->neighbors as $neighbor){
                    /**
                     * @since 2023-12-28 附近点映射
                     */
                    $val = $neighbor->val;
                    if(!isset($nodeMap[$val])){
                        $nodeMap[$val] = new Node($val);
                        $nextNodeArray[] = $neighbor;
                    } else {}
                    $neighborArray[] = $nodeMap[$val];
                }

                /**
                 * @since 2023-12-28 当前点映射
                 */
                $cloneNode = $nodeMap[$nodeArray[$i]->val];

                /**
                 * @since 2023-12-28 建立附近点的关系
                 */
                $cloneNode->neighbors = $neighborArray;
            }

            /**
             * @since 2023-12-28 下次遍历
             */
            $nodeArray = $nextNodeArray;
        }

        /**
         * @return Node
         */
        return $nodeMap[$node->val];
    }
}


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.28_3.php
 */
$datas = [

];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
